package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.Exception.CustomException;
import cn.wolfcode.p2p.Exception.ErrorCode;
import cn.wolfcode.p2p.base.service.ISendMessageService;
import cn.wolfcode.p2p.util.*;
import cn.wolfcode.p2p.vo.UserInfoVo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@Service
public class SendMessageServiceImpl implements ISendMessageService {


    @Override
    public void sendMessage(String phoneNumber) {
        AssertUtil.instance().isMobile(phoneNumber, ErrorCode.USERNAME_FORMAT_ERROR);
        Date date = new Date();
        UserInfoVo temp = UserContext.getUserInfoVo();
        //验证发送验证码是否过于频繁
        if((temp != null) && (DateUtil.getSecondsBetween(date,temp.getLastSendMessageDate()) < Constant.SendMessageMinTime)){
            throw new CustomException(ErrorCode.SENDMESSAGE_FREQUENT);
        }
        String verifycode = UUID.randomUUID().toString().substring(0,4);
        /*//http://utf8.api.smschinese.cn/?Uid=本站用户名&Key=接口安全秘钥&smsMob=手机号码&smsText=验证码:8888
            HashMap map = new HashMap<>();
            map.put("Uid",Constant.SEND_UID);
            map.put("Key",Constant.SEND_KEY);
            map.put("smsMob",phoneNumber);
            map.put("smsText","您的验证码为:"+verifycode+",请在5分钟内完成注册");
        try {
            String s = HttpUtil.sendHttpRequest(Constant.SEND_URL, map);
            AssertUtil.instance().responseResult(s);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setLastSendMessageDate(date);
        userInfoVo.setPhoneNumber(phoneNumber);
        userInfoVo.setVerifycode(verifycode);
        UserContext.setUserInfoVo(userInfoVo);
        System.out.println(verifycode);
    }
}
